﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace UserModule
{
    [XmlInclude(typeof(UserSetting))]
    public class UserSetting
    {
        public string strOperatorPW = "000000";
        public string strTechPW = "111111";
        public string strEngineerPW = "123456";
        public string strAdministatorPW = "654321";
        public string strCodePW = "Worldcode123456";

        public UserSetting()
        {
            strOperatorPW = "000000";
            strTechPW = "111111";
            strEngineerPW = "123456";
            strAdministatorPW = "654321";
            strCodePW = "";
        }
        public static UserSetting Load()
        {
            UserSetting pDoc = null;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(UserSetting));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/UserSetting" + ".xml");
                pDoc = (UserSetting)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
            }
            catch (IOException eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new UserSetting();
            }
            catch (Exception eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                MessageBox.Show(eMy.ToString() + "即将打开备份文件", Application.StartupPath + @"/MuduleParameter/UserSetting" + ".xml");

                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/UserSetting" + ".xml.bak");
                pDoc = (UserSetting)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();

                pDoc.Save();
            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(Application.StartupPath + @"/MuduleParameter/"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"/MuduleParameter/");
            }

            FileStream fsWriter = new FileStream(Application.StartupPath + @"/MuduleParameter/UserSetting" + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(UserSetting));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

            File.Copy(Application.StartupPath + @"/MuduleParameter/UserSetting" + ".xml", Application.StartupPath + @"/MuduleParameter/UserSetting" + ".xml.bak", true);
            //FileStream fsWriter1 = new FileStream(WorldGeneral.AppParam.AppParamPath + @"/MuduleParameter/UserSetting" + ".xml.bak", FileMode.Create, FileAccess.Write, FileShare.Read);
            //XmlSerializer xmlSerializer1 = new XmlSerializer(typeof(UserSetting));
            //xmlSerializer1.Serialize(fsWriter1, this);
            //fsWriter1.Close();

            return true;
        }
    }
}
